package me.lc.linklink;

import me.lc.linklink.core.GameSystem;
import me.lc.linklink.core.MyWiGameAllClient;
import me.lc.linklink.scene.LoadingScene;
import me.lc.linklink.util.AppControl;
import me.lc.linklink.util.DianJinTool;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.admogo.AdMogoManager;
import com.kuguo.ad.KuguoAdsManager;
import com.mobclick.android.MobclickAgent;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;
import com.wiyun.engine.opengl.WYGLSurfaceView;
import com.wiyun.engine.utils.ResolutionIndependent;
import com.wiyun.game.WiGame;

public class MainActivity extends Activity implements
		IDirectorLifecycleListener {
	// load lib
	static {
		System.loadLibrary("wiskia");
		System.loadLibrary("xml2");
		System.loadLibrary("wiengine");
		System.loadLibrary("wisound");
	}

	WYGLSurfaceView mSurfaceView = null;
	private boolean mStarted = false;
	private MyWiGameAllClient wiGameAllClient;

	public static MainActivity instance = null;

	private static int defaultHeight = 800;
	private static int defalutWidth = 480;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		MobclickAgent.onError(instance);
		DianJinTool.init();

		// 设置窗口状态
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏

		GameSystem.AndroidContext = this;
		mSurfaceView = new WYGLSurfaceView(this);
		mSurfaceView.setKeepScreenOn(true);
		setContentView(mSurfaceView);
		// 设置显示帧率，程序发布时应该去掉
		// Director.getInstance().setDisplayFPS(true);

		// 缩放模式
		Director.getInstance().setScaleMode(
				Director.SCALE_MODE_BASE_SIZE_FIT_XY);
		Director.getInstance().setBaseSize(defalutWidth, defaultHeight);

		Director.getInstance().addLifecycleListener(this);
		AppControl.init();
	}

	@Override
	protected void onResume() {
		super.onResume();
		Director.getInstance().resume();
		MobclickAgent.onResume(instance);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(instance);
	}

	@Override
	protected void onStop() {
		super.onStop();
		Director.getInstance().pause();
	}

	@Override
	protected void onDestroy() {
		Director.getInstance().end();
		AdMogoManager.clear();
		WiGame.removeWiGameClient(wiGameAllClient);
		DianJinTool.destroy();
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	public void onDirectorEnded() {

	}

	@Override
	public void onDirectorPaused() {

	}

	@Override
	public void onDirectorResumed() {

	}

	@Override
	public void onDirectorScreenCaptured(String arg0) {

	}

	@Override
	public void onSurfaceChanged(int w, int h) {
		if (!mStarted) {
			mStarted = true;
			float objectTotalHeight = ResolutionIndependent
					.resolveDp(GameSystem.BLOCK_SIZE * GameSystem.GameRows);
			// 50dp是用来放广告的
			GameSystem.Screen_ActuallyHeight = defaultHeight;
			GameSystem.Screen_Height = defaultHeight - 75;
			// GameSystem.Screen_Height = h -
			// ResolutionIndependent.resolveDp(50);
			GameSystem.Screen_Width = defalutWidth;
			GameSystem.Start_Height = (GameSystem.Screen_Height - objectTotalHeight) / 2;
			Director.getInstance().runWithScene(LoadingScene.make());
		}

	}

	@Override
	public void onSurfaceCreated() {

	}

	@Override
	public void onSurfaceDestroyed() {

	}

	public void initWiGame() {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				WiGame.init(MainActivity.this, GameSystem.WiGameAppKey,
						GameSystem.WiGameSecretKey, "1.0", false);
				// 设置wigame回调
				wiGameAllClient = new MyWiGameAllClient();
				WiGame.addWiGameClient(wiGameAllClient);

			}
		});

	}

	public void showExitTips() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog alertDialog = new AlertDialog.Builder(
						MainActivity.this)
						.setTitle("退出")
						.setMessage("是否退出Super连连看")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										// 酷果
										KuguoAdsManager paManager = KuguoAdsManager
												.getInstance();
										paManager
												.setCooId(
														MainActivity.instance,
														"28140ad641764f21904497f0ac0f1a76");
										paManager.receivePushMessage(
												MainActivity.instance, true);
										MainActivity.this.finish();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								}).setCancelable(false).create(); // 创建对话框
				alertDialog.show();
			}
		});
	}

}